<?php
namespace Admin\Controller;
use Think\Controller;
class ReplyproductController extends PublicController {
	
	protected $obj_product;
	
	public function index(){
	
		//搜索变量
		$tuijian=$this->htmlentities_u8($_GET['tuijian']);
		$name=$this->htmlentities_u8($_GET['name']);

		$brand_list = M('Brand')->select();

		$where="status=1";
		$tuijian!='' ? $where.=" AND brand=$tuijian" : null;
		$name!='' ? $where.=" AND pname like '%$name%'" : null;
		define('rows',10);
		$count=M('sp_product')->where($where)->count();
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$products=M('sp_product')->where($where)->limit($limit,rows)->select();		
		foreach ($products as $key => $value) {
			foreach ($brand_list as $k => $v) {
				if($v['id']==$value['brand']){
					$products[$key]['brand_name'] = $v['name'];
				}
			}
			
		}
		$this->assign("brand_list",$brand_list);
		$this->assign("products",$products);
		$this->assign('page',$page);	
		$this->assign('page_index',$page_index);			
		$this->display();
	}
	
	protected function _list(){	
		$where_ands	=	array();
		$keyword	=	cookie('c_keyword');
		$keyword != ''?array_push($where_ands,"pname LIKE '%".$keyword."%'"):'';
		$brand	=	0;
		$brand	=	intval(cookie('c_brand'));
		array_push($where_ands,"brand=$brand");			
		$where	=	join(" and ", $where_ands);	
		if($brand){
			$count	=	$this->obj_product->where($where)->count();
		}else{
			if($keyword){
				$count	=	$this->obj_product->where("pname LIKE '%".$keyword."%'")->count();
				
				
			}else{
				$count	=	$this->obj_product->count();
			}
			
		}		
		
		$PageLink = array(
			'index'=>'/admin/product/index.html',
			'list' =>'/admin/product/index.html?p={page}'
		);
		$page	=	$this->page($count, 10, 1, 6,'',$PageLink,true);
		if($brand){
			$products	=	$this->obj_product->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
		}else{
			if($keyword){
			
				$products	=	$this->obj_product->where("pname LIKE '%".$keyword."%'")->limit($page->firstRow . ',' . $page->listRows)->select();
				
			}else{
				//echo 456;
				$products	=	$this->obj_product->limit($page->firstRow . ',' . $page->listRows)->select();
				
			}
			
		}

		
		$this->assign("products",$products);	
		$this->assign("Page", $page->show('Admin'));
	}
	
	public function _getTree($brandid=0){
		$brands = M('Brand')->select();
		foreach ($brands as $k=>$v) {	
			$selected = $brandid == $k['id'] ? 'selected' : '';
			$str.="<option value='".$k['id']."' $selected>".$v['name']."</option>";
		}
		$this->assign("brandlist", $str);
	}
	
	public function add() {
		$brand_list = M('Brand')->select();
		if (intval($_GET['pid'])) {
			$id = intval($_GET['pid']);
		
			$reply_product = M('sp_product')->where('id='.intval($id))->find();

			$this->assign('reply_product',$reply_product);
			$this->assign('id',$id);
		}
		$this->assign("brand_list",$brand_list);
		$this->display();
	}
	
	public function add_post() {

		if($_POST['submit']==true){
			//如果不是管理员则查询商家会员的店铺ID
			$id = intval($_POST['id']);
			$array=array(
				'pname'=>$_POST['pname'] ,
				'brand'=>$_POST['brand'] ,
				'price'=> $_POST['price']//所属店铺
			);
			  
			//上传产品小图
			if (!empty($_FILES["file"]["tmp_name"])) {
					//文件上传
					$info = $this->upload_images($_FILES["file"],array('jpg','png','jpeg'),"relyproduct/".date(Ymd));
				    if(!is_array($info)) {// 上传错误提示错误信息
				        $this->error($info);
				        exit();
				    }else{// 上传成功 获取上传文件信息
					    $array['avatar'] = '/Data/UploadFiles/'.$info['savepath'].$info['savename'];
					    $xt = M('sp_product')->where('id='.intval($id))->field('avatar')->find();
					    if ($id && $xt['avatar']) {
					    	$img_url = $xt['file'];
							if(file_exists($img_url)) {
								@unlink($img_url);
							}
					    }
				    }
			}
			//执行添加
			if(intval($id)>0){
				$sql = M('sp_product')->where('id='.intval($id))->save($array);
			}else{
				$array['status']=1;
				$sql = M('sp_product')->add($array);
				$id=$sql;
			}

			//规格操作
			if($sql){//name="guige_name[]
				$this->success('操作成功.');
				exit();
			}else{
				$this->error('操作失败.');
				exit();
			}
		}


		// if (IS_POST) {
		// 	if ($this->obj_product->create()) {
		// 		if ($this->obj_product->add()!==false) {
		// 			$this->success("添加成功！", U("Product/index"));
		// 		} else {
		// 			$this->error("添加失败！");
		// 		}
		// 	} else {
		// 		$this->error($this->obj_product->getError());
		// 	}
		// }
	}	
	
	public function edit() {
		$pid=intval($_REQUEST["pid"]);
		$obj = $this->obj_product->where(array("id"=>$pid))->find();
		if($obj){
			$this->assign($obj);
			$this->_getTree($obj['brand']);
			$this->display();			
		}else{
			$this->error('数据不存在',U('Product/index'));
		}
	}	
	public function edit_post() {
		var_dump($_POST);die;
		if (IS_POST) {			
			if ($this->obj_product->create()) {
				if ($this->obj_product->save()!==false) {			
					$this->success("修改成功！", U("Product/index"));
				} else {
					$this->error("失败！");
				}
			} else {
				$this->error($this->obj_product->getError());
			}
		}
	}
	
	public function attribute(){
		$pid=intval($_REQUEST["pid"]);
		$this->assign("pid",$pid);
		$this->display();		
	}
	
	public function set_attribute(){

		if(IS_POST){
			$id = intval($_POST['id']);
			$obj["content"] = json_encode($_POST['content']);
			$sql = M('sp_product')->where('id='.intval($id))->save($obj);	
			
			$this->ajaxReturn(sp_ajax_return("","修改成功",1));
		}
	}

	public function copy_attribute(){
		if(IS_POST){
			$newid = intval($_POST['newid']);			
			$result = M('Product')->where(array('id'=>$newid))->find();
			if($result)
			{
				$obj["id"] = intval($_POST['id']);
				$obj["content"] = $result['content'];	
				if ($this->obj_product->save($obj) !== false) {
					$this->ajaxReturn(sp_ajax_return("","修改成功",1));
				} else {
					$this->ajaxReturn(sp_ajax_return("","修改失败",1));
				}
			}
			else{
				$this->ajaxReturn(sp_ajax_return("","属性未找到",1));
			}

			
		}
	}
	
	public function get_attribute(){
		$pid = intval($_POST['id']);
		$obj=M('sp_product')->where(array("id"=>$pid))->find();
		if($obj){			
			$this->ajaxReturn(sp_ajax_return(json_decode($obj['content']),"yes",1),'JSON');		
		}else{
			$this->ajaxReturn(sp_ajax_return("","no",1));
		}
	}

//============================回收订单开始===================
/*
*回收订单列表
*/
	//=======回收订单列表
	public function order(){
		$this->re_list();
		$this->display();
	}
	protected function re_list(){	
		$where_ands = array();	
		$keyword=isset($_GET['keyword'])?trim($_GET['keyword']):'';	
		$status=isset($_GET['status'])? $_GET['status'] :'';	
		$type=isset($_GET['type'])? $_GET['type'] :'';	
		if ($keyword!='') {
			array_push($where_ands,"pname LIKE '%".$keyword."%'");
			$this->assign('keyword', $keyword);
		}
		if ($status!='') {
			array_push($where_ands,"way=$status");	
			$this->assign('status', $status);
		}	
		if ($type!='') {
				array_push($where_ands,"status=$type");	
				$this->assign('type', $type);
			
		}
		$where = join(" and ", $where_ands);	
		define('rows',10);		
		$count=M('sp_order_temp')->where($where)->count();
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$order=M('sp_order_temp')->where($where)->order('ctime desc')->limit($limit,rows)->select();	
		// echo "<pre>";
		// print_r($order);die;	
		$this->assign('page',$page);	
		$this->assign('page_index',$page_index);		
		$this->assign("order",$order);				
	}
	//确认收货
	 public function update() {
		$ids = $_REQUEST["id"];
		$data['status']=0;
		$Order_youji = M("sp_order_temp");
		if ($Order_youji->where(array("id"=>$ids))->save($data)!==false) {
			$this->success("确认成功！");
		} else {
			$this->error("确认失败！");
		}	     
    }
    //收款确认
	public function price() {
		$ids = $_REQUEST["id"];
		$data['status']=1;
		$Order_youji = M("sp_order_temp");
		if ($Order_youji->where(array("id"=>$ids))->save($data)!==false) {
			$this->success("确认成功！");
		} else {
			$this->error("确认失败！");
		}	     
    }
    //反馈信息
	public function edit_posts() {
		$ids = $_REQUEST["id"];
		$merchant_feedback = $_POST['merchant_feedback'];
		$data['merchant_feedback']=$merchant_feedback;
		$Order_youji = M("sp_order_temp");
		if ($Order_youji->where(array("id"=>$ids))->save($data)!==false) {
			$this->success("确认成功！");
		} else {
			$this->error("确认失败！");
		}	     
    }

    public function detail(){
		$id=$_REQUEST["id"];		
		$result = M("sp_order_temp")->where(array("id"=>$id))->find();
		//var_dump($result);
		if($result){			
			foreach(json_decode($result['order_json'],true) as $k=>$v){		
				$html .="<li class=\"list-group-item\">$k :$v</li>\n";
				
			}		
			$order_json_html = "<ul class=\"list-group\">$html</ul>";	
			
			$result['content'] = $order_json_html;
			$this->assign($result);
			$this->display();			
		}else{
			$this->error('数据不存在',U('Replyproduct/order'));
		}	
	}
	//卖家信息
	public function user(){
		$id=$_REQUEST["id"];		
		$result = M("sp_order_youji")->where(array("orderid"=>$id))->find();
		//var_dump($result);
		//$result = $this->obj->where(array("id"=>$id))->find();
		//var_dump($result);
		
		if($result){			
				if($result['method'] == ''){
					$result['method'] = '银行卡';
				}else{
					$result['method'] = '支付宝';
				}
			$html = "<li class=\"list-group-item\">订单号:".$result['orderid']."</li>\n<li class=\"list-group-item\">用户id:".$result['id']."</li>\n<li class=\"list-group-item\">收款方式:".$result['method']."</li>\n<li class=\"list-group-item\">收款人:".$result['account_name']."</li>\n<li class=\"list-group-item\">收款账号:".$result['account_id']."</li>\n<li class=\"list-group-item\">快递单号:".$result['express']."</li>\n<li class=\"list-group-item\">快递名称:".$result['way']."</li>\n";
				
			$order_json_html = "<ul class=\"list-group\">$html</ul>";	
			
			$result['content'] = $order_json_html;
			$this->assign($result);
			$this->display();			
		}else{
			$temp = M("sp_order_temp")->where(array("id"=>$id))->find();
			$shangmen = M("sp_order_shangmen")->where(array("orderid"=>$id))->find();
			//var_dump($temp);
			//var_dump($shangmen);
			$html = "<li class=\"list-group-item\">订单号:".$id."</li>\n<li class=\"list-group-item\">用户地址:".$shangmen['address']."</li>\n<li class=\"list-group-item\">用户姓名:".$shangmen['uname']."</li>\n<li class=\"list-group-item\">用户电话:".$temp['mobile']."</li>\n<li class=\"list-group-item\">下单时间:".$temp['ctime']."</li>\n";
				
			$order_json_html = "<ul class=\"list-group\">$html</ul>";	
			
			$result['content'] = $order_json_html;
			$this->assign($result);
			$this->display();	
		}	
	}
	//==================回收订单结束=====



}